using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Provausio.Common.Data;
using Provausio.Common.Objects;
using TrainingManagement.api.Models.Data.Readers;

namespace TrainingManagement.api.Models.Data.CRUD.Skill
{
    public class SkillSelectDataSource : QuerySource<int?,List<BusinessObjects.Skill>>
    {
        /// <summary>
        /// Returns a list of Skills. If SkillID is not provided, all skills will be returned.
        /// </summary>
        /// <param name="request">SkillID</param>
        /// <returns></returns>
        public override List<BusinessObjects.Skill> Execute(int? request)
        {
            const string selectSkill = "dbo.training_api_SkillSelect";
            var parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@SkillID", SqlDbType.Int){ Value = request};
            using (var rdr = SqlHelper.ExecuteReader(
                ConnectionStrings.TrainingDb,
                CommandType.StoredProcedure,
                selectSkill,
                parameters))
            {
                var results = new List<BusinessObjects.Skill>();
                var skillReader = new SkillDataReader();
                while (rdr.Read())
                {
                    results.Add(skillReader.Run(rdr));
                }
                return results;
            }
        }
    }
}